package com.bl._02xhr;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;

/**
 *  生产者
 */
public class Producer {

    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        // 1 获取producer对象
        DefaultMQProducer producer = new DefaultMQProducer("xhr_product_group");
        // 2 设置nameserver地址链接
        producer.setNamesrvAddr("localhost:9876");
        // 3 启动
        producer.start();
        // 4 创建消息对象，设置消息
        Message message = new Message("xhr", "hello", "学个基尔".getBytes(StandardCharsets.UTF_8));

        // 4.2.发送消息
       producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("消费成功！！！！"+sendResult.toString());
            }

            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("消费失败！！！！！");
            }
        });

        // 5 关闭
       // producer.shutdown();
    }

}
